import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_order.dart';

class OrderCenterController extends GetxController {
  int totalCount = 0;
  int totalPage = 0;
  int currPage = 1;
  int pageSize = 20;
  bool loading = false;
  List<OrderBean> orderList = [];

  //0 全部
  int _currentTabIndex = 0;
  bool confiroLoading = false;

  int allCount = 0;
  int sendCount = 0;
  int finishCount = 0;

  Future<bool> requestGoodsList(
      {bool isUpdate = false, bool isLoading = true}) async {
    loading = isLoading;
    confiroLoading = false;
    if (isUpdate) {
      update(["orderList"]);
    }
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> map2 = {};
    map2["mineId"] = userBean.mineId;
    map2["tabIndex"] = _currentTabIndex;
    map2["page"] = currPage;
    map2["size"] = pageSize;
    int userId = userBean.userId;

    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.orderListUrl, queryParameters: map2);
    List<OrderBean> themList = [];
    loading = false;
    if (responseInfo.success) {
      //解析数据
      dynamic data = responseInfo.data['page'];
      totalCount = data["totalCount"] ?? 0;
      totalPage = data["totalPage"] ?? 0;
      currPage = data["currPage"] ?? 0;
      pageSize = data["pageSize"] ?? 0;
      dynamic list = data["list"];
      if (list != null) {
        for (dynamic item in list) {
          OrderBean bean = OrderBean.fromMap(item);
          themList.add(bean);
        }
      }

      allCount = responseInfo.data["allCount"] ?? 0;
      sendCount = responseInfo.data["sendCount"] ?? 0;
      finishCount = responseInfo.data["finishCount"] ?? 0;
    }
    if (currPage == 1) {
      orderList = themList;
    } else {
      if (themList.length > 0) {
        orderList.addAll(themList);
      }
    }
    _isLoadingMore = false;
    update(["orderList"]);
    return true;
  }

  Future<bool> onPullRefresh() async {
    currPage = 1;
    await requestGoodsList(isUpdate: true);
    return true;
  }

  Future<bool> onRefresh({int index = 0}) async {
    currPage = 1;
    _currentTabIndex = index;
    await requestGoodsList(isUpdate: true);
    return true;
  }

  void confirm(OrderBean item) async {
    dynamic reault = await Get.defaultDialog(
      radius: 4,
      title: "温馨提示",
      content: Container(
        child: Text("您确认已取到货吗？需要将取货码提供给管理员核销订单"),
      ),
      confirm: Container(
        alignment: Alignment.center,
        child: TextButton(
          onPressed: () {
            Get.back(result: true);
          },
          child: Text(
            "确认",
            style: TextStyle(color: Colors.blue),
          ),
        ),
        width: 100,
      ),
      cancel: Container(
        alignment: Alignment.center,
        child: TextButton(
            onPressed: () {
              Get.back(result: false);
            },
            child: Text("取消")),
        width: 100,
      ),
    );

    if (reault == null || !reault) {
      return;
    }
    confiroLoading = true;
    update(["confirmButton${item.id}"]);
    Map<String, dynamic> map2 = {};
    map2["orderId"] = item.id;
    ResponseInfo responseInfo = await DioUtils.instance
        .postRequest(url: HttpHelper.confirmOrderUrl, jsonMap: map2);
    if (responseInfo.success) {
      //解析数据
      ToastUtils.showToast("已确认取货");
    }
    confiroLoading = false;
    update(["confirmButton${item.id}"]);
    requestGoodsList();
  }

  bool _isLoadingMore = false;

  void loadingMore() {
    if (orderList.length < pageSize) {
      LogUtils.e("不满一页 无须加载更多");
      return;
    }
    if (currPage >= totalPage) {
      LogUtils.e("暂无更多");
      return;
    }
    if (!_isLoadingMore) {
      _isLoadingMore = true;
      currPage++;
      requestGoodsList(isLoading: false);
    }
  }
}
